//
//  swglViewController.m
//  tongxiangOASystem
//
//  Created by michael on 2018/4/19.
//  Copyright © 2018年 czd. All rights reserved.
//

#import "swglViewController.h"
#import "swdaishouViewController.h"
#import "swyishouViewController.h"
#import "swdengjiViewController.h"
#import "SectionChooseView.h"

@interface swglViewController ()<UIScrollViewDelegate,SectionChooseVCDelegate>
@property (nonatomic, strong) UIScrollView *contentScrollView;

@property(nonatomic,strong)SectionChooseView *sectionChooseView;
@property (nonatomic,strong)ViewController *currentVC;
@property (nonatomic,strong)ViewController *oldvc;

@end

@implementation swglViewController

- (void)viewDidLoad {
    [super viewDidLoad];

        self.view.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showFirstVC) name:@"ABC" object:nil];
    
    //添加所有子控制器
    [self setupChildViewController];
    
    //初始化UIScrollView
    [self setupUIScrollView];
//    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"新建"style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
//
//                     self.navigationItem.rightBarButtonItem = item1;
    // Do any additional setup after loading the view.

}

- (void)showFirstVC {
    
    [self showVc:0];
}
- (void)setupUIScrollView {

    // 创建底部滚动视图
    self.contentScrollView = [[UIScrollView alloc] init];
    _contentScrollView.frame = CGRectMake(0, 0, KScreenWidth, self.view.height-kTopHeight-44-LL_TabbarSafeBottomMargin);
//        _contentScrollView.frame = CGRectMake(0, 44, KUIScreenWidth, KUIScreenHeight-kTopHeight);

    _contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3, 0);
    _contentScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _contentScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _contentScrollView.bounces = NO;
    // 隐藏水平滚动条
    _contentScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _contentScrollView.delegate = self;
    [self.view addSubview:_contentScrollView];
    
        self.sectionChooseView = [[SectionChooseView alloc] initWithFrame:CGRectMake(0, _contentScrollView.bottom, self.view.frame.size.width, 44) titleArray:@[@"待收文件", @"已收文件", @"收文登记簿"]];


    self.sectionChooseView.selectIndex = 0;
    self.sectionChooseView.delegate = self;
//    self.sectionChooseView.normalBackgroundColor = [UIColor whiteColor];
//    self.sectionChooseView.selectBackgroundColor = [UIColor orangeColor];;
//    self.sectionChooseView.titleNormalColor = [UIColor orangeColor];
//    self.sectionChooseView.titleSelectColor = [UIColor whiteColor];
    self.sectionChooseView.normalTitleFont = 14;
    self.sectionChooseView.selectTitleFont = 16;
    [self.view addSubview:self.sectionChooseView];
    
}

#pragma mark -添加所有子控制器

-(void)setupChildViewController {
           UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    swdaishouViewController *voucherUnusedVC = [[swdaishouViewController alloc] init];
voucherUnusedVC.selectRowBlock = ^(NSString *rowTitle) {
self.title = rowTitle;
};
    [self addChildViewController:voucherUnusedVC];
    


    swyishouViewController *voucherUsedVC = [[swyishouViewController alloc] init];
    voucherUsedVC.selectRowBlock = ^(NSString *rowTitle) {
self.title = rowTitle;
};
    [self addChildViewController:voucherUsedVC];
    
 
    
    swdengjiViewController *lajiUsedVC = [[swdengjiViewController alloc] init];
      lajiUsedVC.selectRowBlock = ^(NSString *rowTitle) {
self.title = rowTitle;
};
    [self addChildViewController:lajiUsedVC];
    
}

#pragma mark -SMCustomSegmentDelegate

- (void)SectionSelectIndex:(NSInteger)selectIndex {
    
    NSLog(@"---------%ld",(long)selectIndex);
    
    // 1 计算滚动的位置
    CGFloat offsetX = selectIndex * self.view.frame.size.width;
    self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 2.给对应位置添加对应子控制器
    [self showVc:selectIndex];
    
}
- (void)rightButtonAction{

            [[NSNotificationCenter defaultCenter] postNotificationName:@"gaoji" object:nil]; //刷新发件箱，收件箱 数据

}
#pragma mark -显示控制器的view
/**
 *  显示控制器的view
 *
 *  @param index 选择第几个
 *
 */
- (void)showVc:(NSInteger)index {
     if(index==1){
      UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"高级搜索"style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];

                     self.navigationItem.rightBarButtonItem = item1;
    
    }else{
                         self.navigationItem.rightBarButtonItem = nil;

    }
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
//    _oldvc = (ViewController *)vc;
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
//    if (vc.isViewLoaded) return;
for(UIView *subv in [self.contentScrollView subviews])
{
[subv removeFromSuperview];
}

    [self.contentScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark -UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 1.添加子控制器view
    [self showVc:index];
    
    // 2.把对应的标题选中
    self.sectionChooseView.selectIndex = index;
    
    
}
- (void)replaceFromOldViewController:(UIViewController *)oldVc toNewViewController:(UIViewController *)newVc{
    /**
     *  transitionFromViewController:toViewController:duration:options:animations:completion:
     *  fromViewController    当前显示在父视图控制器中的子视图控制器
     *  toViewController        将要显示的姿势图控制器
     *  duration                动画时间(这个属性,old friend 了 O(∩_∩)O)
     *  options              动画效果(渐变,从下往上等等,具体查看API)UIViewAnimationOptionTransitionCrossDissolve
     *  animations            转换过程中得动画
     *  completion            转换完成
     */
    [self addChildViewController:newVc];
    [self transitionFromViewController:oldVc toViewController:newVc duration:0.1 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        if (finished) {
            [newVc didMoveToParentViewController:self];
            [oldVc willMoveToParentViewController:nil];
            [oldVc removeFromParentViewController];
            self.currentVC = newVc;
        }else{
            self.currentVC = oldVc;
        }
    }];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end


